<template>
  <div class="app-container">
    <!-- <el-button type="primary" @click="handleAddRole">New Role</el-button> -->
    <div id="">
      <p class="bian_hao_text">结算单号:FS4444444444</p>
      <div id="" class="he_tong_xin_xi">
        <p>合同编号：SF-00001</p>
        <p>合同名称:合同名称合同名称合同名称</p>
        <p>委托单位:合同名称合同名称合同名称</p>
        <p>项目名称:合同名称合同名称合同名称</p>
      </div>
      <el-table show-summary :data="rolesList" style="width: 100%;margin-top:30px;" border>
        <el-table-column type="selection" width="55" align="center"></el-table-column>
        <el-table-column align="center" label="项目名称" width="220" >
          <template slot-scope="scope">
            {{ scope.row.name }}
          </template>
        </el-table-column>
        <el-table-column align="center" label="业务类型" width="220">
          <template slot-scope="scope">
            {{ scope.row.type }}
          </template>
        </el-table-column>
        <el-table-column align="center" label="工作量">
          <template slot-scope="scope">
            {{ scope.row.wordload }}
          </template>
        </el-table-column>
        <el-table-column align="center" label="单价" prop='unit_price'>
          <!-- <template slot-scope="scope">
           {{ scope.row.unit_price }}
          </template> -->
        </el-table-column>
        <el-table-column align="center" label="总价" prop='total_prices'>
         <!-- <template slot-scope="scope">
            {{ scope.row.total_prices }}
          </template> -->
        </el-table-column>
        <el-table-column align="center" label="付款结算单状态">
          <template slot-scope="scope">
            {{ scope.row.state }}
          </template>
        </el-table-column>
        <el-table-column align="center" label="付款结算单位">
          <template slot-scope="scope">
            {{ scope.row.unit }}
          </template>
        </el-table-column>
        <el-table-column align="center" label="日期">
          <template slot-scope="scope">
            {{ scope.row.time }}
          </template>
        </el-table-column>
      </el-table>
      <div id="" class="bottom_zong_e">
        <p class="he_ji">合计总额：100</p>
        <p>应开票金额：100</p>
      </div>

    </div>
    <!-- 附属结算单号 -->
    <div id="">
      <p class="bian_hao_text">附属结算单号：SF-00001</p>
      <el-table show-summary :data="rolesList" style="width: 100%;margin-top:30px;" border>
        <el-table-column type="selection" width="55" align="center"></el-table-column>
        <el-table-column align="center" label="项目名称" width="220" >
          <template slot-scope="scope">
            {{ scope.row.name }}
          </template>
        </el-table-column>
        <el-table-column align="center" label="业务类型" width="220">
          <template slot-scope="scope">
            {{ scope.row.type }}
          </template>
        </el-table-column>
        <el-table-column align="center" label="工作量">
          <template slot-scope="scope">
            {{ scope.row.wordload }}
          </template>
        </el-table-column>
        <el-table-column align="center" label="单价" prop='unit_price'>
          <!-- <template slot-scope="scope">
           {{ scope.row.unit_price }}
          </template> -->
        </el-table-column>
        <el-table-column align="center" label="总价" prop='total_prices'>
         <!-- <template slot-scope="scope">
            {{ scope.row.total_prices }}
          </template> -->
        </el-table-column>
        <el-table-column align="center" label="付款结算单状态">
          <template slot-scope="scope">
            {{ scope.row.state }}
          </template>
        </el-table-column>
        <el-table-column align="center" label="付款结算单位">
          <template slot-scope="scope">
            {{ scope.row.unit }}
          </template>
        </el-table-column>
        <el-table-column align="center" label="日期">
          <template slot-scope="scope">
            {{ scope.row.time }}
          </template>
        </el-table-column>
      </el-table>
      <div id="" class="bottom_zong_e">
        <p class="he_ji">合计总额：100</p>
      </div>

    </div>
    <!-- 附属结算单号 -->
    <!-- 发票管理 -->
    <div id="">
      <p class="bian_hao_text">发票管理</p>
      <el-table show-summary :data="fa_piao_guan_li" style="width: 100%;margin-top:30px;" border>
        <el-table-column type="selection" width="55" align="center"></el-table-column>
        <el-table-column align="center" label="发票流水号" width="220">
          <template slot-scope='scope'>
            {{scope.row.liu_shui_hao}}
          </template>
        </el-table-column>
        <el-table-column align="center" label="发票名称" width="220" prop='name'>

        </el-table-column>
        <el-table-column align="center" label="金额" width="220" prop='money'>

        </el-table-column>
        <el-table-column align="center" label="备注" width="937" prop='msg'>

        </el-table-column>
      </el-table>
    </div>
    <!-- 发票管理 -->
    <!-- 回款管理 -->
    <div id="">
      <div id="" class="btn">
        <p class="bian_hao_text">回款管理</p>
      <!--  <div id="" class="button_add_del">
          <el-button type="primary">添加</el-button>
          <el-button type="danger">删除</el-button>
        </div> -->
      </div>
      <el-table show-summary :data="rolesList" style="width: 100%;margin-top:30px;" border>
        <el-table-column type="selection" width="55" align="center"></el-table-column>
        <el-table-column align="center" label="序号" width="220">

        </el-table-column>
        <el-table-column align="center" label="回款单号" width="200">

        </el-table-column>
        <el-table-column align="center" label="金额" width="200">

        </el-table-column>
        <el-table-column align="center" label="额外成本" width="">

        </el-table-column>
        <el-table-column align="center" label="加产值" width="">

        </el-table-column>
        <el-table-column align="center" label="扣产值" width="">

        </el-table-column>
        <el-table-column align="center" label="勘测服务费" width="">

        </el-table-column>
        <el-table-column align="center" label="备注" width="240">

        </el-table-column>
      </el-table>
      <!-- 底部是否添加提醒 -->
      <div id="" class="bottom_remind">
        <p>是否添加提醒</p>
        <div id="" class="remind_radio">
          <el-radio v-model="radio" size='medium' :border='true' label="1">是</el-radio>
          <el-radio v-model="radio" size='medium' :border='true' label="2">否</el-radio>
        </div>
      </div>
      <!-- 底部是否添加提醒 -->
    </div>
    <!-- 回款管理 -->
    <!-- 提醒日期 -->
    <div id="" class="remind_time">
      <el-badge is-dot class="item">提醒日期</el-badge>
      <el-date-picker class='time_select' v-model="remind_time" type="datetime" placeholder="选择日期时间">
      </el-date-picker>
    </div>
    <!-- 提醒日期 -->
    <el-dialog :visible.sync="dialogVisible" :title="dialogType==='edit'?'Edit Role':'New Role'">
      <el-form :model="role" label-width="80px" label-position="left">
        <el-form-item label="Name">
          <el-input v-model="role.name" placeholder="Role Name" />
        </el-form-item>
        <el-form-item label="Desc">
          <el-input v-model="role.description" :autosize="{ minRows: 2, maxRows: 4}" type="textarea"
            placeholder="Role Description" />
        </el-form-item>
        <el-form-item label="Menus">
          <el-tree ref="tree" :check-strictly="checkStrictly" :data="routesData" :props="defaultProps" show-checkbox
            node-key="path" class="permission-tree" />
        </el-form-item>
      </el-form>
      <div style="text-align:right;">
        <el-button type="danger" @click="dialogVisible=false">Cancel</el-button>
        <el-button type="primary" @click="confirmRole">Confirm</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import path from 'path'
  import {
    deepClone
  } from '@/utils'
  import {
    getRoutes,
    getRoles,
    addRole,
    deleteRole,
    updateRole
  } from '@/api/role'

  const defaultRole = {
    key: '',
    name: '',
    description: '',
    routes: []
  }

  export default {
    data() {
      return {
        // 提醒日期
        remind_time: '',
        // 单选框
        radio: '1',
        role: Object.assign({}, defaultRole),
        routes: [],
        // 发票管理
        fa_piao_guan_li:[
            {
                liu_shui_hao:'42145',
                name:'哈哈哈',
                money:50,
                msg:'俺说的把你家登记卡不好的'
            },

        ],
        rolesList: [{
            name:'qqqq',
            type:'qqqq',
            wordload:'qqqq',
            unit_price:100,
            total_prices:200,
            state:'qqqq',
            unit:'qqqq',
            time:'2011-1-1'
          }
        ],
        dialogVisible: false,
        dialogType: 'new',
        checkStrictly: false,
        defaultProps: {
          children: 'children',
          label: 'title'
        }
      }
    },
    computed: {
      routesData() {
        return this.routes
      }
    },
    created() {
     
      // Mock: get all routes and roles list from server
      // this.getRoutes()
      // this.getRoles()
    },
    methods: {
      async getRoutes() {
        const res = await getRoutes()
        this.serviceRoutes = res.data
        this.routes = this.generateRoutes(res.data)
      },
      async getRoles() {
        const res = await getRoles()
        this.rolesList = res.data
      },

      // Reshape the routes structure so that it looks the same as the sidebar
      generateRoutes(routes, basePath = '/') {
        const res = []
        for (let route of routes) {
          // skip some route
          if (route.hidden) {
            continue
          }

          const onlyOneShowingChild = this.onlyOneShowingChild(route.children, route)

          if (route.children && onlyOneShowingChild && !route.alwaysShow) {
            route = onlyOneShowingChild
          }

          const data = {
            path: path.resolve(basePath, route.path),
            title: route.meta && route.meta.title
          }

          // recursive child routes
          if (route.children) {
            data.children = this.generateRoutes(route.children, data.path)
          }
          res.push(data)
        }
        return res
      },
      generateArr(routes) {
        let data = []
        routes.forEach(route => {
          data.push(route)
          if (route.children) {
            const temp = this.generateArr(route.children)
            if (temp.length > 0) {
              data = [...data, ...temp]
            }
          }
        })
        return data
      },
      handleAddRole() {
        this.role = Object.assign({}, defaultRole)
        if (this.$refs.tree) {
          this.$refs.tree.setCheckedNodes([])
        }
        this.dialogType = 'new'
        this.dialogVisible = true
      },
      handleEdit(scope) {
        this.dialogType = 'edit'
        this.dialogVisible = true
        this.checkStrictly = true
        this.role = deepClone(scope.row)
        this.$nextTick(() => {
          const routes = this.generateRoutes(this.role.routes)
          this.$refs.tree.setCheckedNodes(this.generateArr(routes))
          // set checked state of a node not affects its father and child nodes
          this.checkStrictly = false
        })
      },
      handleDelete({
        $index,
        row
      }) {
        this.$confirm('Confirm to remove the role?', 'Warning', {
            confirmButtonText: 'Confirm',
            cancelButtonText: 'Cancel',
            type: 'warning'
          })
          .then(async () => {
            await deleteRole(row.key)
            this.rolesList.splice($index, 1)
            this.$message({
              type: 'success',
              message: 'Delete succed!'
            })
          })
          .catch(err => {
            console.error(err)
          })
      },
      generateTree(routes, basePath = '/', checkedKeys) {
        const res = []

        for (const route of routes) {
          const routePath = path.resolve(basePath, route.path)

          // recursive child routes
          if (route.children) {
            route.children = this.generateTree(route.children, routePath, checkedKeys)
          }

          if (checkedKeys.includes(routePath) || (route.children && route.children.length >= 1)) {
            res.push(route)
          }
        }
        return res
      },
      async confirmRole() {
        const isEdit = this.dialogType === 'edit'

        const checkedKeys = this.$refs.tree.getCheckedKeys()
        this.role.routes = this.generateTree(deepClone(this.serviceRoutes), '/', checkedKeys)

        if (isEdit) {
          await updateRole(this.role.key, this.role)
          for (let index = 0; index < this.rolesList.length; index++) {
            if (this.rolesList[index].key === this.role.key) {
              this.rolesList.splice(index, 1, Object.assign({}, this.role))
              break
            }
          }
        } else {
          const {
            data
          } = await addRole(this.role)
          this.role.key = data.key
          this.rolesList.push(this.role)
        }

        const {
          description,
          key,
          name
        } = this.role
        this.dialogVisible = false
        this.$notify({
          title: 'Success',
          dangerouslyUseHTMLString: true,
          message: `
            <div>Role Key: ${key}</div>
            <div>Role Name: ${name}</div>
            <div>Description: ${description}</div>
          `,
          type: 'success'
        })
      },
      // reference: src/view/layout/components/Sidebar/SidebarItem.vue
      onlyOneShowingChild(children = [], parent) {
        let onlyOneChild = null
        const showingChildren = children.filter(item => !item.hidden)

        // When there is only one child route, the child route is displayed by default
        if (showingChildren.length === 1) {
          onlyOneChild = showingChildren[0]
          onlyOneChild.path = path.resolve(parent.path, onlyOneChild.path)
          return onlyOneChild
        }

        // Show parent if there are no child route to display
        if (showingChildren.length === 0) {
          onlyOneChild = {
            ...parent,
            path: '',
            noShowingChildren: true
          }
          return onlyOneChild
        }

        return false
      }
    }
  }
</script>

<style lang="scss" scoped>
  .app-container {
    .roles-table {
      margin-top: 30px;
    }

    .permission-tree {
      margin-bottom: 30px;
    }
  }

  .bian_hao_text {
    color: #909399;
  }

  .he_tong_xin_xi {
    margin-left: -20px;
  }

  .he_tong_xin_xi>p {
    float: left;
    margin-left: 20px;
    color: #909399;
  }

  // 底部总额
  .bottom_zong_e {
    display: flex;
  }

  // 合计
  .he_ji {
    margin-right: 20px;
  }

  // 底部是否提示时间
  .bottom_remind {
    display: flex;
    align-items: center;
    margin-top: 30px;
  }

  // 单选按钮
  .remind_radio {
    margin-left: 30px;
    width: 200px;
    display: flex;
    justify-content: space-between;
  }

  .remind_time {
    margin-top: 30px;
  }

  // 日期选择器
  .time_select {
    margin-left: 30px;
  }
  .button {
    margin-top: 30px;
    width: 200px;
    height: 40px;

  }
  .btn{
    display: flex;
    width: 100%;
    justify-content: space-between;
    align-items: center;
    margin-top: 30px;
  }
</style>
